#include <vector>
using namespace std;

class UF {
    vector<int> parent;
    int cnt;

  public:
    UF(int n) {
        cnt = n;
        parent.resize(n);
        for (int i = 0; i < n; i++) parent[i] = i;
    }

    int findParent(int x) {
        if (parent[x] != x) { parent[x] = findParent(parent[x]); }
        return parent[x];
    }

    bool connected(int x, int y) {
        int xp = findParent(x);
        int yp = findParent(y);
        return xp == yp;
    }

    void Union(int x, int y) {
        int xp = findParent(x);
        int yp = findParent(y);
        if (xp == yp) return;
        cnt--;
        parent[yp] = xp;
        return;
    }

    int getCount() { return cnt; }
};
